///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IMAGE_ALGORITHM_ARITHMETIC_H_
#define _BUOLA_IMAGE_ALGORITHM_ARITHMETIC_H_

#include <buola/image/algorithm/detail/opencv.h>

namespace buola { namespace img {

//! \name arithmetic operations
//@{

template<typename tFormat>
inline void abs_diff(const CView<tFormat> &p1,const CView<tFormat> &p2,const CMView<tFormat> &pDst)
{
    cv::absdiff(mat_wrap(p1),mat_wrap(p2),mat_wrap(pDst));
}

template<typename tFormat>
inline void accumulate(const CView<tFormat> &pSrc,const CMView<tFormat> &pDst)
{
    cv::accumulate(mat_wrap(pSrc),mat_wrap(pDst));
}

template<typename tF1,typename tF2,typename tF3>
inline void add(const CView<tF1> &p1,const CView<tF2> &p2,const CMView<tF3> &pDst)
{
    cv::add(mat_wrap(p1),mat_wrap(p2),mat_wrap(pDst),cv::noArray(),MFormatToCVType<tF3>::mMatType);
}

template<typename tF1,typename tF2,typename tF3>
inline void sub(const CView<tF1> &p1,const CView<tF2> &p2,const CMView<tF3> &pDst)
{
    cv::subtract(mat_wrap(p1),mat_wrap(p2),mat_wrap(pDst),cv::noArray(),MFormatToCVType<tF3>::mMatType);
}

///pSrc=pScale*(pSrc+pAdd)
void add_and_scale(const CMView_gray32f &pSrc,float pAdd,float pScale);

///pSrc=pSrc^1.5
void pow1p5(const CMView_gray32f &pSrc);

template<typename tF1,typename tF2,typename tF3>
inline void add_weighted(const CView<tF1> &p1,double pA1,const CView<tF2> &p2,double pA2,
                         const CMView<tF3> &pDst,double pGamma=0.0)
{
    cv::addWeighted(mat_wrap(p1),pA1,mat_wrap(p2),pA2,pGamma,mat_wrap(pDst),
                    MFormatToCVType<tF3>::mMatType);
}

template<typename tFormat,typename tAcc>
inline void running_average(const CView<tFormat> &pSrc,double pAlpha,const CMView<tAcc> &pDst)
{
    cv::accumulateWeighted(mat_wrap(pSrc),mat_wrap(pDst),pAlpha);
}

template<typename tFormat,typename tAcc,typename tMask>
inline void running_average(const CView<tFormat> &pSrc,double pAlpha,const CMView<tAcc> &pDst,
                            const CView<tMask> &pMask)
{
    cv::accumulateWeighted(mat_wrap(pSrc),mat_wrap(pDst),pAlpha,mat_wrap(pMask));
}

template<typename tFormat>
inline void bitwise_or(const CView<tFormat> &p1,const CView<tFormat> &p2,const CMView<tFormat> &pDst)
{
    cv::bitwise_or(mat_wrap(p1),mat_wrap(p2),mat_wrap(pDst));
}

template<typename tFormat>
inline void bitwise_and(const CView<tFormat> &p1,const CView<tFormat> &p2,const CMView<tFormat> &pDst)
{
    cv::bitwise_and(mat_wrap(p1),mat_wrap(p2),mat_wrap(pDst));
}

template<typename tFormat,typename tMask>
inline void apply_binary_mask(const CView<tFormat> &pSrc,const CView<tMask> &pMask,const CMView<tFormat> &pDst,
                              int pThreshold=20,int pFailValue=0)
{
    for(int y=0;y<pSrc.Height();++y)
    {
        auto lSrcIt=pSrc.RowBegin(y);
        auto lDstIt=pDst.RowBegin(y);
        auto lMaskIt=pMask.RowBegin(y);

        for(int x=0;x<pSrc.Width();++x)
        {
            if(lMaskIt[x][0]>pThreshold)
            {
                for(int c=0;c<pSrc.cChannels;++c)
                {
                    lDstIt[x][c]=lSrcIt[x][c];
                }
            }
            else
            {
                for(int c=0;c<pSrc.cChannels;++c)
                {
                    lDstIt[x][c]=pFailValue;
                }
            }
        }
    }
}

template<typename tFormat,typename tMask>
inline void apply_binary_mask(const CMView<tFormat> &pSrc,const CView<tMask> &pMask,
                            int pThreshold=20,int pFailValue=0)
{
    for(int y=0;y<pSrc.Height();++y)
    {
        auto lSrcIt=pSrc.RowBegin(y);
        auto lMaskIt=pMask.RowBegin(y);

        for(int x=0;x<pSrc.Width();++x)
        {
            if(lMaskIt[x][0]<=pThreshold)
            {
                for(int c=0;c<pSrc.cChannels;++c)
                {
                    lSrcIt[x][c]=pFailValue;
                }
            }
        }
    }
}

//@}
    
/*namespace image*/ } /*namespace buola*/ }

#endif
